﻿#ifndef __INC_QG_D11_CNV_H__
#define __INC_QG_D11_CNV_H__

#ifdef _MSC_VER
#pragma once
#pragma warning(push)
#pragma warning(disable:4624)
#endif

namespace D11Convert
{
	// color format
	inline DXGI_FORMAT DetectColor(qgColorFormat& fmt, bool& alpha)
	{
		// D11은 16비트 24비트 컬러를 지원하지 않기 땜시 32비트로 바꾸거나 취소
		// 알파없는 컬러도 지원하지 않기 때문에 다 알파 쓴다
		// D11.1에서 16비트 컬러 지원...하지만 쓸일은 없지.
		switch (fmt)
		{
			case QGCF8_L:
				alpha = false;
				return DXGI_FORMAT_R8_UNORM;

			case QGCF16_L:
				alpha = false;
				return DXGI_FORMAT_R16_UNORM;

			case QGCF32_BGR:
				// 지원안함 BGRA로

			case QGCF32_BGRA:
				fmt = QGCF32_BGRA;
				alpha = true;
				return DXGI_FORMAT_R8G8B8A8_UNORM;

			case QGCF16_RGB:
				// 지원안함 32RGB로

			case QGCF32_RGB:
				fmt = QGCF32_RGB;
				alpha = false;
				return DXGI_FORMAT_B8G8R8X8_UNORM;

			case QGCF16_RGBA:
				// 지원안함 32RGBA로 

			case QGCF32_RGBA:	// DXGI1.1용 즉 D11용
				fmt = QGCF32_RGBA;
				alpha = true;
				return DXGI_FORMAT_B8G8R8A8_UNORM;

			case QGCF16F_R:
				alpha = false;
				return DXGI_FORMAT_R16_FLOAT;

			case QGCF64F_BGRA:
				alpha = true;
				return DXGI_FORMAT_R16G16B16A16_FLOAT;

			case QGCF32F_R:
				alpha = false;
				return DXGI_FORMAT_R32_FLOAT;

			case QGCF128F_BGRA:
				alpha = true;
				return DXGI_FORMAT_R32G32B32A32_FLOAT;

			case QGCF_DXT1:
				alpha = false;
				return DXGI_FORMAT_BC1_UNORM;

			case QGCF_DXT2:
				alpha = false;
				return DXGI_FORMAT_BC2_UNORM;

			case QGCF_DXT3:
				alpha = false;
				return DXGI_FORMAT_BC3_UNORM;

			case QGCF_DXT4:
				alpha = false;
				return DXGI_FORMAT_BC4_UNORM;

			case QGCF_DXT5:
				alpha = false;
				return DXGI_FORMAT_BC5_UNORM;

			default:
				return DXGI_FORMAT_UNKNOWN;
		}
	}

	//
	inline qgColorFormat ColorFormat(DXGI_FORMAT fmt)
	{
		switch (fmt)
		{
			case DXGI_FORMAT_R8_UNORM:
				return QGCF8_L;

			case DXGI_FORMAT_R16_UNORM:
				return QGCF16_L;

			case DXGI_FORMAT_R8G8B8A8_UNORM:
				return QGCF32_BGRA;

			case DXGI_FORMAT_B8G8R8A8_UNORM:	// DXGI1.1용 즉 D11용
				return QGCF32_RGBA;

			case DXGI_FORMAT_B8G8R8X8_UNORM:
				return QGCF32_RGB;

			case DXGI_FORMAT_R16_FLOAT:
				return QGCF16F_R;

			case DXGI_FORMAT_R16G16B16A16_FLOAT:
				return QGCF64F_BGRA;

			case DXGI_FORMAT_R32_FLOAT:
				return QGCF32F_R;

			case DXGI_FORMAT_R32G32B32A32_FLOAT:
				return QGCF128F_BGRA;

			case DXGI_FORMAT_BC1_UNORM:
				return QGCF_DXT1;

			case DXGI_FORMAT_BC2_UNORM:
				return QGCF_DXT2;

			case DXGI_FORMAT_BC3_UNORM:
				return QGCF_DXT3;

			case DXGI_FORMAT_BC4_UNORM:
				return QGCF_DXT4;

			case DXGI_FORMAT_BC5_UNORM:
				return QGCF_DXT5;

			default:
				return QGCF_NONE;
		}
	}

	// 
	inline bool TestCompressedFormat(DXGI_FORMAT fmt)
	{
		switch (fmt)
		{
			case DXGI_FORMAT_BC1_UNORM:
			case DXGI_FORMAT_BC2_UNORM:
			case DXGI_FORMAT_BC3_UNORM:
			case DXGI_FORMAT_BC4_UNORM:
			case DXGI_FORMAT_BC5_UNORM:
				return true;
		}

		return false;
	}

	// 
	inline D3D11_PRIMITIVE_TOPOLOGY Topology(qgTopology tpg)
	{
		return (D3D11_PRIMITIVE_TOPOLOGY)tpg;
	}

	//
	inline D3D11_COMPARISON_FUNC ComparisonOp(qgCmpOp cop)
	{
		return (D3D11_COMPARISON_FUNC)cop;
	}

	//
	inline D3D11_BLEND_OP BlendOp(qgBlendOp op)
	{
		return (D3D11_BLEND_OP)op;
	}

	//
	inline D3D11_BLEND BlendValue(qgBlendValue v)
	{
		return (D3D11_BLEND)v;
	}

	//
	inline D3D11_FILTER Filter(qgFilter v)
	{
		switch (v)
		{
			case QGFLT_MAG_MIN_MIP_POINT:
				return D3D11_FILTER_MIN_MAG_MIP_POINT;

			case QGFLT_MAG_MIN_POINT_MIP_LINEAR:
				return D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;

			case QGFLT_MAG_POINT_MIN_LINEAR_MIP_POINT:
				return D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT;

			case QGFLT_MAG_POINT_MIN_MIP_LINEAR:
				return D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;

			case QGFLT_MAG_LINEAR_MIN_MIP_POINT:
				return D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;

			case QGFLT_MAG_LINEAR_MIN_POINT_MIP_LINEAR:
				return D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;

			case QGFLT_MAG_MIN_LINEAR_MIP_POINT:
				return D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;

			case QGFLT_MAG_MIN_MIP_LINEAR:
				return D3D11_FILTER_MIN_MAG_MIP_LINEAR;

			case QGFLT_ANISOTROPIC:
				return D3D11_FILTER_ANISOTROPIC;

			case QGFLTX_POINT:
				return D3D11_FILTER_MIN_MAG_MIP_POINT;

			case QGFLTX_LINEAR:
				return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
		}

		return D3D11_FILTER_MIN_MAG_MIP_POINT;	// 기본값
	}

	//
	inline D3D11_TEXTURE_ADDRESS_MODE Address(qgAddress v)
	{
		return (D3D11_TEXTURE_ADDRESS_MODE)v;
	}
};

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#endif	// __INC_QG_D11_CNV_H__
